package itsm.isperp.module.controller.change;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.entity.change.ChgElementRelation;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.change.ChgChangeService;
import itsm.isperp.module.service.change.ChgElementRelationService;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 服务控制器
 * 
 * @author zhg
 * 
 */
@Controller
@RequestMapping("change/elementRelation")
public class ChgChangElementRelationController extends BaseController<ChgElementRelation> {

	/**
	 * 
	 */
	@Autowired
	protected ChgElementRelationService ChgElementRelationService;

	@Autowired
	protected AppDictService appDictService;

	@Override
	public ChgElementRelationService getService() {
		return ChgElementRelationService;
	}

	@Override
	public String getPageTitle() {
		return "";
	}

	@RequestMapping("listByFilter")
	@ResponseBody
	public PageResponse<ChgElementRelation> listByFilter(JqGridRequest request, HttpServletRequest req) {
		String sourceId = req.getParameter("id");
		String ty = req.getParameter("ty");
		request.addFilterRule("to_type", JqGridFilterRuleOp.eq, ty);
		request.addFilterRule("from_id", JqGridFilterRuleOp.eq, sourceId);
		return this.getService().findAll(request).toPage();

	}

	@RequestMapping("getOneFromViewRelation")
	@ResponseBody
	public Map<String, Object> getOneFromViewRelation(JqGridRequest request, HttpServletRequest req) {
		String sourceId = req.getParameter("id");
		String toType = req.getParameter("ty");
		request.addFilterRule("from_id", JqGridFilterRuleOp.eq, sourceId);
		request.addFilterRule("to_type", JqGridFilterRuleOp.eq, toType);
		return this.getService().findOneDetailByRequest(request);

	}

	@RequestMapping("insertMailToByUserName")
	@ResponseBody
	public String insertMailToByUserName(JqGridRequest request, HttpServletRequest req) {

		String mailToNames = req.getParameter("names");
		String relationType = req.getParameter("ty");
		String sourceId = req.getParameter("id");
		String[] namesArr = mailToNames.split(",");
		AppUserService appUserService = ContextHolder.getSpringBean("appUserService");
		for (String s : namesArr) {
			AppUser u = appUserService.findByUsername(s, "fullname", "email");
			ChgElementRelation entity = new ChgElementRelation();
			entity.setFromId(sourceId);
			// 根据用户名获取用户信息
			entity.setName(u.getFullname());
			entity.setToId(u.getEmail());
			entity.setToType(relationType);
			JqGridRequest request2 = new JqGridRequest();
			request2.setSearchString(" from_id='" + sourceId + "' and to_id='" + u.getEmail() + "' and to_type='" + relationType + "' ");
			ChgElementRelation tempObj = this.getService().findOneByRequest(request2);
			if (tempObj == null)
				this.getService().saveOrUpdate(entity); 
		}
		return "ok";
	}

	@RequestMapping("saveChgElementRelation")
	@ResponseBody
	public String saveChgElementRelation(JqGridRequest request, HttpServletRequest req) {

		String toId = req.getParameter("to_id");
		String fromId = req.getParameter("from_id");
		String type = req.getParameter("ty");
		String name = req.getParameter("name");
		ChgElementRelation entity = new ChgElementRelation();
		entity.setFromId(fromId);
		// 根据用户名获取用户信息
		entity.setName(name);
		entity.setToId(toId);
		entity.setToType(type);
		JqGridRequest request2 = new JqGridRequest();
		request2.setSearchString(" from_id='" + fromId + "' and to_id='" + toId + "' and to_type='" + type + "' ");
		ChgElementRelation tempObj = this.getService().findOneByRequest(request2);
		if (tempObj == null)
			this.getService().saveOrUpdate(entity); 
		return "ok";
	}
	
	@RequestMapping("insertChgEvRelateIcd")
	@ResponseBody
	public String insertChgEvRelateIcd(JqGridRequest request, HttpServletRequest req) {

		String toId = req.getParameter("to_id");
		String fromId = req.getParameter("from_id");
		String type = req.getParameter("ty");
		String name = req.getParameter("name");
		ChgElementRelation entity = new ChgElementRelation();
		entity.setFromId(fromId);
		entity.setName(name);
		entity.setToId(toId);
		entity.setToType(type);
		JqGridRequest request2 = new JqGridRequest();
		request2.setSearchString(" from_id='" + fromId + "' and to_id='" + toId + "' and to_type='" + type + "' ");
		ChgElementRelation tempObj = this.getService().findOneByRequest(request2);
		if (tempObj == null)
			this.getService().saveOrUpdate(entity); 
		ChgChangeService chgChangeService = ContextHolder.getSpringBean("chgChangeService");
		Map<String,Object> rc=chgChangeService.findEvaluationRelateIcd(fromId);
		return rc.get("count_c").toString();
	}
	
	@RequestMapping("cancelChgEvRelateIcd")
	@ResponseBody
	public String cancelChgEvRelateIcd(JqGridRequest request, HttpServletRequest req) {

		String toId = req.getParameter("to_id");
		String fromId = req.getParameter("from_id");
		this.getService().deleteAllByFilter(fromId, toId);
		
		ChgChangeService chgChangeService = ContextHolder.getSpringBean("chgChangeService");
		Map<String,Object> rc=chgChangeService.findEvaluationRelateIcd(fromId);
		return rc.get("count_c").toString();
	}

	@RequestMapping("changeAddSentToEdit")
	public ModelAndView changeAddSentToEdit() {
		ModelAndView mv = new ModelAndView("change/change/changeAddSentToEdit");
		return mv;
	}
}
